package com.example.study1206.Entity;


import com.example.study1206.service.NewServiceimp;

public class Requirement {
    private Object[] attributes;
    private Condition[] conditions;
    private String tableName;
    private int pageNum;
    private int pageSize;

    public Requirement(Object[] attributes, Condition[] conditions, String tableName, int pageNum, int pageSize) {
        this.attributes = attributes;
        this.conditions = conditions;
        this.tableName = tableName;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }


    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void setAttributes(Object[] attributes) {
        this.attributes = attributes;
    }

    public Condition[] getConditions() {
        return conditions;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setConditions(Condition[] conditions) {
        this.conditions = conditions;
    }

    public String getTableName() throws InjectionException {
        if(tableName!=null&&NewServiceimp.sql_inj(tableName)){
            throw new InjectionException();
        }
        return tableName;
    }

    public Object[] getAttributes() {
        return attributes;
    }

    public String getFormalizedAttributes() throws InjectionException {
        String r = "";
        if(attributes ==null){
            return "";
        }
        for (Object k : this.attributes) {

            r += k.toString() + ",";
        }
        if(NewServiceimp.sql_inj(r.substring(0, r.length() - 1))&&!r.substring(0, r.length() - 1).equals("*")){
            throw new InjectionException();
        }
        return r.substring(0, r.length() - 1);
    }

    public String getFormalizedConditions() throws InjectionException {
        String r = "";
        if (conditions != null) {
            for (Condition c : conditions) {
                String cform ="";
                cform = c.formalize();
                r += cform;
            }
            if (!r.equals("")) {
                return "Where " + r;
            }
            return r;
        }
        return r;
    }

    public String getPageInfo() {
        if (this.pageNum <= 0 || pageSize <= 0) {
            return "limit 0";

        }
        return "limit " + (pageNum - 1) * pageSize + ", " + pageSize;

    }

}
